package com.springcloud.consumercourse.controller;

import com.entity.ResponseResultSet;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author
 * @create 2020-11-10 23:23
 * 熔断测试
 */
@RestController
public class HystrixController {
    @HystrixCommand(fallbackMethod = "testFallBack",commandProperties = {
        @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),  //打开断路器
        @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "8"),         //请求次数
        @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),  //窗口时间
        @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "50"),  //错误率
    })

    @RequestMapping("/test/{id}")
    public ResponseResultSet test(@PathVariable("id") Integer id){
        if(id >4){
           return new ResponseResultSet(200,"success","Hystrix Ok");
        }else{
            throw  new RuntimeException("请将id的值设置为大于4");
        }
    }

    public ResponseResultSet testFallBack(@PathVariable("id") Integer id){
        return new ResponseResultSet(500,"error","testFallBack");
    }
}
